package cli;

import client.SocketClient;
import dto.ActionTypeEnum;
import picocli.CommandLine;
import service.Node;
import utils.ConfigLoader;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@CommandLine.Command(name = "client", description = "操作数据库")
public class ClientCommand implements Runnable{

    @CommandLine.Option(names = {"-c", "--config"}, required = true, description = "配置文件路径")
    private String configPath;

    @CommandLine.Option(names = {"-t", "--table"}, required = true, description = "表名")
    private String table;

    @CommandLine.Option(names = {"--key"}, required = true, description = "键名")
    private String key;

    @CommandLine.Option(names = {"--value"}, description = "值 ->仅 SET, MSET 操作需要")
    private Object value;

    @CommandLine.Option(names = {"--action"}, required = true, description = "操作类型: set, get, rm, mset, mget",
            converter = ActionConverter.class)
    private ActionTypeEnum action;


    @Override
    public void run() {
        try {
            List<Node> nodes = ConfigLoader.loadNodes(configPath);
            SocketClient client = new SocketClient(nodes);

            switch (action) {
                case SET:
                    if (value == null) {
                        throw new CommandLine.ParameterException(new CommandLine(this),
                                "SET 操作必须提供 --value 参数");
                    }
                    client.set(table, key, value);
                    System.out.println("设置成功");
                    break;
                case GET:
                    Object result = client.get(table, key);
                    System.out.println("值: " + result);
                    break;
                case RM:
                    client.rm(table, key);
                    System.out.println("删除成功");
                    break;
                case MSET:
                    if (value == null) {
                        throw new CommandLine.ParameterException(new CommandLine(this),
                                "SET 操作必须提供 --value 参数");
                    }
                    String[] keys = key.split(",");
                    String[] values = value.toString().split(",");
                    List<String> klist = Arrays.asList(keys);
                    List<Object> vlist = Arrays.asList(values);
                    HashMap<String, Object> map = new HashMap<>();
                    for (int i = 0; i < klist.size(); i++) {
                        map.put(klist.get(i), vlist.get(i));
                    }
                    client.mSet(table, map);
                    System.out.println("批量增改成功");
                    break;
                case MRM:
                    String[] arr = key.split(",");
                    List<String> keyList = Arrays.asList(arr);
                    client.mRemove(table, keyList);
                    System.out.println("批量删除成功");
                    break;
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
        }
    }

    // 类型转换器
    static class ActionConverter implements CommandLine.ITypeConverter<ActionTypeEnum> {
        @Override
        public ActionTypeEnum convert(String value) {
            return ActionTypeEnum.valueOf(value.toUpperCase());
        }
    }
}

